

#include"stdafx.h"
#include<stdio.h>
#include<math.h>
#include<windows.h>
#include<gl/GL.h>
#include<stdlib.h>
#include<GL/glut.h>
float theta[]={0.0,0.0};
int axis=1,p=0,count=18;
GLfloat X = 000.0f; // Translate screen to x direction (left or right)
GLfloat Y = 0002.6f; // Translate screen to y direction (up or down)
GLfloat Z = 000.0f; // Translate screen to z direction (zoom in or out)
GLfloat r=0.0;
void load_texture2 ( char *file_name, int width, int height, int depth, GLenum colour_type, GLenum filter_type )
{
	GLubyte *raw_bitmap ;
	FILE *file;
	if ((file = fopen(file_name, "rb"))==NULL )
	{
		printf ( "File Not Found : %s\n", file_name );
		exit   ( 1 );
	}

	raw_bitmap = (GLubyte *) malloc ( width * height * depth * ( sizeof(GLubyte)) );

	if (raw_bitmap == NULL)
	{
		printf ( "Cannot allocate memory for texture\n" );
		fclose ( file);
		exit   ( 1 );
	}

	fread  ( raw_bitmap , width * height * depth, 1 , file );
	fclose ( file);

	//  Set Filtering type
	glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_type );
	glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_type );

	//  Set Texture Evironment
	glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

	//  Build Mipmaps
	gluBuild2DMipmaps ( GL_TEXTURE_2D, colour_type, width, height,
		colour_type, GL_UNSIGNED_BYTE, raw_bitmap );

	//  Free up the array
	free ( raw_bitmap );
}

void drawwater()
{
	//draw water texture
	glColor3f(0,0.4,.8);
	glBegin(GL_POLYGON);
	glVertex3f(-1.0,0.0,-1.0);
	glVertex3f( 1.0,0.0,-1.0);
	glVertex3f( 1.0,0.0, 1.0);
	glVertex3f(-1.0,0, 1.0);
	glEnd();	
}
void drawbox()
{  // Enable material properties for lighting
	glEnable ( GL_COLOR_MATERIAL );
	glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
	glEnable ( GL_TEXTURE_2D );
	//glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
	load_texture2 ( "wood1.raw", 256, 256, 3, GL_RGB, GL_LINEAR );
	glEnable (GL_DEPTH_TEST);
	glColor3f(0.9, 0.4,0);
	glBegin(GL_QUADS);
	// back inner wall
	glTexCoord2f(0, 0.85);  glVertex3f(-1, -0.1, -1);
	glTexCoord2f(1, 0.85);  glVertex3f( 1, -0.1, -1);
	glTexCoord2f(1, 1.00);  glVertex3f( 1,  0.2, -1);
	glTexCoord2f(0, 1.00);  glVertex3f(-1,  0.2, -1);

	// back outer wall
	glTexCoord2f(0, 0.8);  glVertex3f(-1.1, -0.2, -1.1);
	glTexCoord2f(1, 0.8);  glVertex3f( 1.1, -0.2, -1.1);
	glTexCoord2f(1, 1.0);  glVertex3f( 1.1,  0.2, -1.1);
	glTexCoord2f(0, 1.0);  glVertex3f(-1.1,  0.2, -1.1);

	// front left outer wall
	glTexCoord2f(0.00, 0.8);  glVertex3f(-1.1, -0.2, -1.0);
	glTexCoord2f(0.03, 0.8);  glVertex3f(-1.1, -0.2, -1.1);
	glTexCoord2f(0.03, 1.0);  glVertex3f(-1.1,  0.2, -1.1);
	glTexCoord2f(0.00, 1.0);  glVertex3f(-1.1,  0.2, -1.0);
	// front right outer wall
	glTexCoord2f(0.00, 0.8);  glVertex3f(1.1, -0.2, -1.0);
	glTexCoord2f(0.03, 0.8);  glVertex3f(1.1, -0.2, -1.1);
	glTexCoord2f(0.03, 1.0);  glVertex3f(1.1,  0.2, -1.1);
	glTexCoord2f(0.00, 1.0);  glVertex3f(1.1,  0.2, -1.0);

	// back top wall
	glTexCoord2f(0, 1.0);  glVertex3f(-1.1, 0.2, -1.0);
	glTexCoord2f(1, 1.0);  glVertex3f( 1.1, 0.2, -1.0);
	glTexCoord2f(1, 0.9);  glVertex3f( 1.1, 0.2, -1.1);
	glTexCoord2f(0, 0.9);  glVertex3f(-1.1, 0.2, -1.1);

	// front inner wall
	glTexCoord2f(0, 0.85);  glVertex3f(-1, -0.1, 1);
	glTexCoord2f(1, 0.85);  glVertex3f( 1, -0.1, 1);
	glTexCoord2f(1, 1.00);  glVertex3f( 1,  0.2, 1);
	glTexCoord2f(0, 1.00);  glVertex3f(-1,  0.2, 1);

	// front outer wall
	glTexCoord2f(0, 0.8);  glVertex3f(-1.1, -0.2, 1.1);
	glTexCoord2f(1, 0.8);  glVertex3f( 1.1, -0.2, 1.1);
	glTexCoord2f(1, 1.0);  glVertex3f( 1.1,  0.2, 1.1);
	glTexCoord2f(0, 1.0);  glVertex3f(-1.1,  0.2, 1.1);

	// front left outer wall
	glTexCoord2f(0.00, 0.8);  glVertex3f(-1.1, -0.2, 1.0);
	glTexCoord2f(0.03, 0.8);  glVertex3f(-1.1, -0.2, 1.1);
	glTexCoord2f(0.03, 1.0);  glVertex3f(-1.1,  0.2, 1.1);
	glTexCoord2f(0.00, 1.0);  glVertex3f(-1.1,  0.2, 1.0);

//	 front right outer wall
	glTexCoord2f(0.00, 0.8);  glVertex3f(1.1, -0.2, 1.0);
	glTexCoord2f(0.03, 0.8);  glVertex3f(1.1, -0.2, 1.1);
	glTexCoord2f(0.03, 1.0);  glVertex3f(1.1,  0.2, 1.1);
	glTexCoord2f(0.00, 1.0);  glVertex3f(1.1,  0.2, 1.0);

	// front top wall
	glTexCoord2f(0, 1.0);  glVertex3f(-1.1, 0.2, 1.0);
	glTexCoord2f(1, 1.0);  glVertex3f( 1.1, 0.2, 1.0);
	glTexCoord2f(1, 0.9);  glVertex3f( 1.1, 0.2, 1.1);
	glTexCoord2f(0, 0.9);  glVertex3f(-1.1, 0.2, 1.1);

	// right inner wall
	glTexCoord2f(0, 0.85);  glVertex3f( 1, -0.1, -1);
	glTexCoord2f(1, 0.85);  glVertex3f( 1, -0.1,  1);
	glTexCoord2f(1, 1.00);  glVertex3f( 1,  0.2,  1);
	glTexCoord2f(0, 1.00);  glVertex3f( 1,  0.2, -1);
	// right outer wall
	glTexCoord2f(0, 0.8);  glVertex3f( 1.1, -0.2,-1.0);
	glTexCoord2f(1, 0.8);  glVertex3f( 1.1, -0.2, 1.0);
	glTexCoord2f(1, 1.0);  glVertex3f( 1.1,  0.2, 1.0);
	glTexCoord2f(0, 1.0);  glVertex3f( 1.1,  0.2,-1.0);

	// right top wall
	glTexCoord2f(0, 1.0);  glVertex3f( 1.1, 0.2,-1.0);
	glTexCoord2f(1, 1.0);  glVertex3f( 1.1, 0.2, 1.0);
	glTexCoord2f(1, 0.9);  glVertex3f( 1.0, 0.2, 1.0);
	glTexCoord2f(0, 0.9);  glVertex3f( 1.0, 0.2,-1.0);

	// left inner wall
	glTexCoord2f(0, 0.85);  glVertex3f(-1, -0.1, -1);
	glTexCoord2f(1, 0.85);  glVertex3f(-1, -0.1,  1);
	glTexCoord2f(1, 1.00);  glVertex3f(-1,  0.2,  1);
	glTexCoord2f(0, 1.00);  glVertex3f(-1,  0.2, -1);

	// left outer wall
	glTexCoord2f(0, 0.8);  glVertex3f(-1.1, -0.2,-1.0);
	glTexCoord2f(1, 0.8);  glVertex3f(-1.1, -0.2, 1.0);
	glTexCoord2f(1, 1.0);  glVertex3f(-1.1,  0.2, 1.0);
	glTexCoord2f(0, 1.0);  glVertex3f(-1.1,  0.2,-1.0);

	// left top wall
	glTexCoord2f(0, 1.0);  glVertex3f(-1.1, 0.2,-1.0);
	glTexCoord2f(1, 1.0);  glVertex3f(-1.1, 0.2, 1.0);
	glTexCoord2f(1, 0.9);  glVertex3f(-1.0, 0.2, 1.0);
	glTexCoord2f(0, 0.9);  glVertex3f(-1.0, 0.2,-1.0);

	// bottom wall
	glTexCoord2f(0, 1);  glVertex3f(-1.1,-0.2,-1.1);
	glTexCoord2f(1, 1);  glVertex3f( 1.1,-0.2,-1.1);
	glTexCoord2f(1, 0);  glVertex3f( 1.1,-0.2, 1.1);
	glTexCoord2f(0, 0);  glVertex3f(-1.1,-0.2, 1.1);
	glEnd();
    glDisable(GL_TEXTURE_2D);
}
void object()
{
	glColor3f(0,0.6,0.8);
	glPushMatrix();
	glTranslatef(X,Y,Z);
	glutSolidSphere(0.04,20,20);
	glutPostRedisplay();
	glFlush();
	glPopMatrix();
}
void jug()
{
	glColor3f(0,0,0);
	glPushMatrix();
	glTranslatef(.36,2.8,0);
	glRotatef(180,0,1,0);
	glRotatef(0,1,0,0);
	glRotatef(-30,0,0,1);
	glScalef(1,2,1);
	glutWireTeapot(.2);
	glColor3f(0,0.6,0.8);
	glutSolidTeapot(.2);
	glFlush();
	glutPostRedisplay();
	glPopMatrix();
}
void ripple()
{
	for(float j=0;j<=360.0;j+=0.1)
	{
		glColor3f(0,.5,.8);
		glPushMatrix();
		glRotatef(90,1,0,0);
		glTranslatef(0,0,.0);
		glBegin(GL_POINTS);
		glVertex2f(r*cos(j),r*sin(j));
		glEnd();
		glPopMatrix();

	}
	for(float j=0;j<=360.0;j+=0.1)
	{
		glColor3f(0,.4,.8);
		glPushMatrix();
		glRotatef(90,1,0,0);
		glTranslatef(0,0,.0);
		glBegin(GL_POINTS);
		glVertex2f((r-0.02)*cos(j),(r-.02)*sin(j));
		glEnd();
		glPopMatrix();

	}
	for(float j=0;j<=360.0;j+=0.1)
	{
		glColor3f(0,.5,.8);
		glPushMatrix();
		glRotatef(90,1,0,0);
		glTranslatef(0,0,.0);
		glBegin(GL_POINTS);
		glVertex2f((r-.04)*cos(j),(r-.04)*sin(j));
		glEnd();
		glPopMatrix();

	}
	if((r-.18)>=0)
	{
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,0.5,0.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,0.0);
			glBegin(GL_POINTS);
			glVertex2f((r-0.18)*cos(j),(r-0.18)*sin(j));
			glEnd();
			glPopMatrix();
		}
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,0.4,.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,.0);
			glBegin(GL_POINTS);
			glVertex2f((r-0.02-.18)*cos(j),(r-.02-.18)*sin(j));
			glEnd();
			glPopMatrix();
		}
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,0.5,.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,.0);
			glBegin(GL_POINTS);
			glVertex2f((r-.04-.18)*cos(j),(r-.04-.18)*sin(j));
			glEnd();
			glPopMatrix();
		}
	}
	if((r-.38)>=0)
	{
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,0.5,0.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,0.0);
			glBegin(GL_POINTS);
			glVertex2f((r-0.38)*cos(j),(r-0.38)*sin(j));
			glEnd();
			glPopMatrix();
		}
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,.4,.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,.0);
			glBegin(GL_POINTS);
			glVertex2f((r-0.02-.38)*cos(j),(r-.02-.38)*sin(j));
			glEnd();
			glPopMatrix();
		}
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,.5,.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,.0);
			glBegin(GL_POINTS);
			glVertex2f((r-.04-.38)*cos(j),(r-.04-.38)*sin(j));
			glEnd();
			glPopMatrix();
		}
	}
	if((r-0.58)>=0)
	{
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,0.5,0.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,0.0);
			glBegin(GL_POINTS);
			glVertex2f((r-0.58)*cos(j),(r-0.58)*sin(j));
			glEnd();
			glPopMatrix();
		}
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,.4,.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,.0);
			glBegin(GL_POINTS);
			glVertex2f((r-0.02-.58)*cos(j),(r-.02-.58)*sin(j));
			glEnd();
			glPopMatrix();
		}
		for(float j=0;j<=360.0;j+=0.1)
		{
			glColor3f(0,.5,.8);
			glPushMatrix();
			glRotatef(90,1,0,0);
			glTranslatef(0,0,.0);
			glBegin(GL_POINTS);
			glVertex2f((r-.04-.58)*cos(j),(r-.04-.58)*sin(j));
			glEnd();
			glPopMatrix();
		}
	}
}
void display1()
{
	object();
	ripple();
}
void display()
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glEnable(GLUT_DEPTH);
	glLoadIdentity();
	glTranslatef(0.0,-1.0,-3);
	glRotatef(theta[0],1,0,0);						
	glRotatef(theta[1],0,1,0);
	jug();
	display1();
	if(count)
	{
		count--;
		r+=0.0002;
	}
	else
	r+=0.05;
	Y-=0.13;
	if(Y<0.001)
		Y=2.6;
	if(Y==2.6)
		r=0;
	if(r>.9)
	{
		Y=2.6;
		r=0;
	}
	drawbox();
	drawwater();
	glFlush();
	glutSwapBuffers();
}
void spin()
{
	if(p==1)
		theta[1]+=2.0;
	if(p==2)
		theta[1]-=2.0;
	if (theta[1] > 360.0) 
		theta[1]-=360;
	glutPostRedisplay();
}
void myinit()
{
	glClearColor(1,.85,.75,0.0);
	glShadeModel(GL_SMOOTH);
	glPointSize(2.5);
	glClearDepth(1.0);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);
	theta[0]=32;
	theta[1]=30;
}
void mouse(int btn,int state,int x,int y)
{ 
	if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) p=1;
	if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) p=2;
	if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) p=0;
	display();
}
void processkeys(unsigned char key,int x,int y)
{
	if(key=='r'||key=='R') p=2;
	if(key=='l'||key=='L') p=1;
	if(key=='s'||key=='S') p=0;
	display();
}

void myshape(int w,int h)
{
	if(h==0) h=1;
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if(w<=h)
		glOrtho(-2.0,2.0,-2.0*(GLfloat) h/(GLfloat) w,2.0*(GLfloat) h/(GLfloat) w,-10.0,10.0);
	else
		glOrtho(-2.0*(GLfloat) w/(GLfloat) h,2.0*(GLfloat) w/(GLfloat) h,-2.0,2.0,-10.0,10.0);
	glMatrixMode(GL_MODELVIEW);
}
void main(int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
	glutInitWindowSize(500,500);
	glutCreateWindow("WATER RIPPLES");
	myinit();
	glutSwapBuffers();
	glutReshapeFunc(myshape);
	glutDisplayFunc(display);
	glutMouseFunc(mouse);
	glutKeyboardFunc(processkeys);
	glutIdleFunc(spin);
	glutMainLoop();
}